package com.xsomnus.network_programming.netty.udp;

import com.xsomnus.network_programming.netty.udp.handler.UdpServerHandler;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelOption;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioDatagramChannel;

/**
 * @author @xsomnus666_xiawenye★
 * @since 2019/7/24 0024 9:51
 * - 才需学也,学需静也/非淡泊无以明志，非宁静无以致远
 */
public class UDPServer {

    public void run(int port) {
        NioEventLoopGroup bossGroup = new NioEventLoopGroup();

        try {
            Bootstrap bootstrap = new Bootstrap();
            bootstrap.group(bossGroup)
                    .channel(NioDatagramChannel.class)
                    .option(ChannelOption.SO_BROADCAST, true)
                    .handler(new UdpServerHandler());

            bootstrap.bind(port).sync().channel().closeFuture().await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            bossGroup.shutdownGracefully();
        }
    }

    public static void main(String[] args) {
        new UDPServer().run(9190);
    }

}
